package com.bjtech.web.action.management;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.ssi.web.struts.bean.UploadFile;

import com.bjtech.dao.AssignDataFileDao;
import com.bjtech.dao.AssignMeterDataDao;
import com.bjtech.dao.SysAccessaryDao;
import com.bjtech.domain.AssignDataFile;
import com.bjtech.domain.SysAccessary;
import com.bjtech.domain.query.AssignDataFileQuerySpec;
import com.bjtech.web.action.LyheatBaseAction;

public class AssignDataFileAction extends LyheatBaseAction {

	private static final long serialVersionUID = 1L;
	private AssignDataFileDao assignDataFileDao;
	private AssignDataFileQuerySpec selectBean;
	
	private AssignMeterDataDao assignMeterDataDao;
	private SysAccessaryDao sysAccessaryDao;
	
	private String custom;
	private List meterDataList;
	
	public void prepare() throws Exception {

	}
	
	public AssignDataFileAction() throws Exception{
		model = new AssignDataFile();
		selectBean = new AssignDataFileQuerySpec();
	}
	
	public void setAssignDataFileDao(AssignDataFileDao assignDataFileDao) {
		this.assignDataFileDao = assignDataFileDao;
	}

	public String execute(){
		return this.list();
	}
	
	public String list(){
		selectBean.queryParamString();
		list = this.assignDataFileDao.selectEntityPaginatedList(new AssignDataFile(),selectBean);
		return "list";
	}
	
	public String add(){
		return "edit";
	}
	
	public String save(){
		AssignDataFile entity = (AssignDataFile)model;
		if(entity.getId()==null){
			assignDataFileDao.insertEntity(entity);
		}else{
			assignDataFileDao.updateEntity(entity);
		}
		return this.list();
	}
	
	public String edit(){
		model = this.assignDataFileDao.selectEntity(new AssignDataFile(),id);
		return "edit";
	}

	public String view(){
		model = this.assignDataFileDao.selectEntity(new AssignDataFile(),id);
		meterDataList = this.assignDataFileDao.selectMeterDataListByFileId(id);
		return "view";
	}
	public String delete(){
		model = this.assignDataFileDao.deleteEntity(new AssignDataFile(),id);
		return this.list();
	}
	
	public String uploadMF(){
		custom = "AssignDataFile/upload.jsp";
		return "custom";
	}
	
	public String uploadMeterFile(){
		AssignDataFile entity = (AssignDataFile)model;
		SysAccessary sysAccessary = new SysAccessary();
		
		for (UploadFile uploadFile : getUploadFileList()) {
			sysAccessary = new SysAccessary();
			sysAccessary.setLink_id(null);
			sysAccessary.setLink_tab("AssignMeterFile");
			sysAccessary.setAc_sname(uploadFile.getFileName());
			sysAccessary.setUrl(uploadFile.getFileSavePath());
			this.sysAccessaryDao.insertEntity(sysAccessary);
			
			entity.setFile_name(uploadFile.getFileName());
			entity.setFile_size((float)uploadFile.getFileSize()/1024);
			entity.setSave_url(uploadFile.getFileSavePath());
			entity.setUpload_date(new Date());
			entity.setUpload_man(super.getUser().getUsername());
		}

		Long file_id=assignDataFileDao.insertEntity(entity);
		entity.setId(file_id);
		
		//读取上传文件
		List<String> meterRecords = readUploadMeterFileData(getUploadFileList().get(0).getFileSavePath());
		entity.setRecord_num(Long.valueOf(meterRecords.size()));
		//将数据导入数据库
		int success_num = this.assignMeterDataDao.insertMeterDatas(meterRecords,file_id);
		entity.setSuccess_num(Long.valueOf(success_num));
		if(meterRecords.size()==0){
			entity.setUpload_per(0f);
		}else{
			entity.setUpload_per(entity.getSuccess_num()*100/entity.getRecord_num().floatValue());
		}
		
		assignDataFileDao.updateEntity(entity);
		
		super.renderJavaScript("window.onload=function(){alert('数据导入成功！');}");
		return "success";
	}
	
	private List<String> readUploadMeterFileData(String fileSavePath) {
		List<String> meterRecords = new ArrayList<String>();
		String ctxDir = getRequest().getSession().getServletContext().getRealPath(String.valueOf(File.separatorChar));
		if (!ctxDir.endsWith(String.valueOf(File.separatorChar))) {
			ctxDir = ctxDir + File.separatorChar;
		}
		File meterFile = new File(ctxDir+fileSavePath);
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(meterFile));
			String meterRecord="";
			while((meterRecord=br.readLine())!=null){
				meterRecords.add(meterRecord);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return meterRecords;
	}
	public AssignDataFileQuerySpec getSelectBean() {
		return selectBean;
	}

	public void setSelectBean(AssignDataFileQuerySpec selectBean) {
		this.selectBean = selectBean;
	}

	public String getCustom() {
		return custom;
	}

	public void setCustom(String custom) {
		this.custom = custom;
	}

	public AssignDataFileDao getAssignDataFileDao() {
		return assignDataFileDao;
	}

	public SysAccessaryDao getSysAccessaryDao() {
		return sysAccessaryDao;
	}

	public void setSysAccessaryDao(SysAccessaryDao sysAccessaryDao) {
		this.sysAccessaryDao = sysAccessaryDao;
	}

	public AssignMeterDataDao getAssignMeterDataDao() {
		return assignMeterDataDao;
	}

	public void setAssignMeterDataDao(AssignMeterDataDao assignMeterDataDao) {
		this.assignMeterDataDao = assignMeterDataDao;
	}

	public List getMeterDataList() {
		return meterDataList;
	}

	public void setMeterDataList(List meterDataList) {
		this.meterDataList = meterDataList;
	}
}
